var qbaoSchema = require("./qbaoSchema");
var qbaoUtil = require("./qbaoUtil");
var qbaoConfig = require("./qbaoConfig");
var moment = require('moment');
var mongoose = require("mongoose");
var cheerio = require("cheerio");

var db = mongoose.connect(qbaoConfig.dbUrl);
var qbaoUrl = qbaoConfig.qbaoUrl;
var pages = qbaoConfig.pages;
var qbaoResult = qbaoSchema.qbaoResult;

moment.locale('zh-cn');


//解析获取后的html数据
function parseHtmlDate(){

    for (var i = 1; i <= pages; i++) {

        var url = qbaoUrl + i;

        qbaoUtil.getHtmlData(url, function(data) {
            if (data) {

                var $ = cheerio.load(data);

                //开始解析数据
                $("div.taskEve").each(function(i, e) {
                    //地址
                    var adderss = $(e).parent("li").attr("data-href");
                    //图片地址
                    var imgSrc = $(e).find("img").attr("src");
                    //领取数
                    var rwGet = $(e).find("span.rwGet").text();
                    //评论数
                    var rwComment = $(e).find("span.rwComment").text();

                    //奖励钱宝数
                    var reward = $(e).find("div.taskSum").text().replace(/(^\s*)|(\s*$)/g, "").replace(/[^0-9]/ig,"");
                    //保证金
                    var cautionMoney = $(e).find("ul.taskAbout").children("li").eq(0).text().replace(/(^\s*)|(\s*$)/g, "").replace(/[^0-9]/ig,"");
                    //罚金
                    var fine = $(e).find("ul.taskAbout").children("li").eq(1).text().replace(/(^\s*)|(\s*$)/g, "").replace(/[^0-9]/ig,"");
                    //天数
                    var days = $(e).find("ul.taskAbout").children("li").eq(2).text().replace(/(^\s*)|(\s*$)/g, "").replace(/[^0-9]/ig,"");
                    //利润
                    var profit = reward/days/cautionMoney*10000;
                    //风险指数
                    var risk = fine/days/cautionMoney*10000;

                    //数据组装赋值
                    var result = new qbaoResult({
                        //url  		   : url,//url
                        id  		   : i,//id
                        //pageNo  	   : "page",//pageNo
                        detailAdderss  : "http://www.qbao.com" + adderss,//地址
                        imgSrc         : imgSrc,//图片地址
                        rwGet          : rwGet, //领取数
                        rwComment      : rwComment,//评论数
                        reward         : reward,//奖励
                        cautionMoney   : cautionMoney,//保证金
                        days   		   : days,//天数
                        fine           : fine,//罚金
                        profit         : profit,//利润
                        risk           : risk,//风险指数
                        createTime     : moment().format(),//插入数据的时间
                        updateTime     : moment().format('MMMM Do YYYY, h:mm:ss a')//更新数据的时间
                    });
                    //保存数据
                    result.save();

                });

                //console.log("done");
            } else {
                console.log("error");
            }
        });
    };
}

exports.parseHtmlDate = parseHtmlDate;